

def merge(A, B):
    len_A = len(A)
    len_B = len(B)
    i, j = 0, 0
    result = []
    while i < len_A and j < len_B:
        if(A[i] <= B[j]):
            result.append(A[i])
            i += 1
        else:
            result.append(B[j])
            j += 1
    result.extend(A[i:])
    result.extend(B[j:])
    return result

def merge_sort(a):
    if len(a) <= 1:
        return a
    len_a = len(a)
    mid = len_a // 2
    left = merge_sort(a[:mid])
    right = merge_sort(a[mid:])
    return merge(left, right)

# Implementing the described algorithm in Python
def has_pair_with_sum(S, x):
    # Step 1: Sort the list S
    S.sort()

    # Step 2: Use two-pointer technique
    left = 0
    right = len(S) - 1

    while left < right:
        current_sum = S[left] + S[right]
        if current_sum == x:
            print('left_index:',left,'right_index:', right)
            return True  # Found the pair
        elif current_sum < x:
            left += 1  # Increase left pointer to get a larger sum
        else:
            right -= 1  # Decrease right pointer to get a smaller sum

    return False  # No pair found

# Example usage:
S = [10, 15, 3, 9, 8, 12]
x = 17
result = has_pair_with_sum(S, x)
print(result)